package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.FlightHelper;

public class ConnectingFlightServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	
	String flightID1 = null;
	String flightID2 = null;
	String username = null;
	String bothFlightID = null;
	String msg = null;
	

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		
		bothFlightID = req.getParameter("r1");
			
		username = req.getParameter("username");
		try
		{
			FlightHelper.Setup(connection);
			
			if (username.equals("null"))//make sure user has logged in
			{
				msg = "You must sign in to reserve a flight.";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("flight/search_error.jsp").forward(req, resp);
			}
			else if (bothFlightID == null || bothFlightID.isEmpty())//make sure user has chosen a flight
			{
				
				msg = "You did not select a flight.";
				req.setAttribute("msg", msg);
				req.setAttribute("username", username);
				req.getRequestDispatcher("flight/search_error.jsp").forward(req, resp);
			}
			else
			{	
				String[] s = bothFlightID.split("/");
				flightID1 = s[0];
				flightID2 = s[1];
				
				String flight1 = "'"+flightID1+"'";
				String flight2 = "'"+flightID2+"'";				
				req.setAttribute("flightID1", flightID1);
				req.setAttribute("flightID2", flightID2);
				req.setAttribute("flight1", flight1);
				req.setAttribute("flight2", flight2);
				req.setAttribute("username", username);
				req.getRequestDispatcher("reservation/reserveConnectingFlight.jsp").forward(req, resp);	
			}
		}
		catch (SQLException e1) 
		{
			msg = "Reservation Failed.";
			e1.printStackTrace();
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);	
		}
	}

}

